﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XRERP.Models;
using XRERP.utils;
using System.Data.Entity;

namespace XRERP.Controllers
{
    /// <summary>
    /// 客户、项目管理
    /// </summary>
    public class CustomerController : Controller
    {

        private MySqlDB db = new MySqlDB();

        

        // GET: Customer
        public ActionResult Index()
        {
            ViewBag.Title = "客户(项目)管理";
            return View();
        }

        /// <summary>
        /// 添加新客户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddCustomer(String customer)
        {
            JR json = new JR();
            if (customer.Equals(null) || customer == "")
            {
                json.code = 500;
                json.msg = "您什么也没有输入!";
                return Json(json);
            }
            try
            {
                customer = HttpUtility.UrlDecode(customer);
                xr_bl_clientinfo info = new xr_bl_clientinfo();
                info.client_name = customer;
                info.clientinfo_uuid = Tools.getGuid();
                info.validflag = "1";
                db.xr_bl_clientinfo.Add(info);
                if (db.SaveChanges() == 1)
                {
                    json.code = 200;
                    json.msg = "保存成功!";
                }
                else
                {
                    json.code = 300;
                    json.msg = "保存失败，请重试或与管理员联系！";
                }
            }
            catch (Exception e)
            {
                json.code = 505;
                json.msg = e.ToString();
            }

            return Json(json);
        }

        /// <summary>
        /// 编辑客户信息
        /// </summary>
        /// <param name="uuid"></param>
        /// <param name="customer"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditCustomer(String uuid, String customer)
        {
            JR json = new JR();
            if (customer.Equals(null) || uuid.Equals(null) || customer == "" || uuid == "")
            {
                json.code = 500;
                json.msg = "请输入有效值";
                return Json(json);
            }
            try
            {
                customer = HttpUtility.UrlDecode(customer);
                xr_bl_clientinfo info = db.xr_bl_clientinfo.FirstOrDefault(x =>x.clientinfo_uuid  == uuid);
                info.client_name = customer;
                db.Entry(info).State = EntityState.Modified;
                if (db.SaveChanges() == 1)
                {
                    json.code = 200;
                    json.msg = "修改成功!";
                }
                else
                {
                    json.code = 300;
                    json.msg = "修改失败，请重试或与管理员联系！";
                }
            }
            catch (Exception e)
            {
                json.code = 505;
                json.msg = e.ToString();
            }

            return Json(json);
        }

        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="uuid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelCustomer(String uuid)
        {
            JR json = new JR();
            if ( uuid.Equals(null) ||  uuid == "")
            {
                json.code = 500;
                json.msg = "请选择要删除的客户";
                return Json(json);
            }
            try
            {
                xr_bl_clientinfo info = db.xr_bl_clientinfo.FirstOrDefault(x => x.clientinfo_uuid == uuid);
                info.validflag="0";
                db.Entry(info).State = EntityState.Modified;
                if (db.SaveChanges() == 1)
                {
                    json.code = 200;
                    json.msg = "删除成功!";
                }
                else
                {
                    json.code = 300;
                    json.msg = "删除失败，请重试或与管理员联系！";
                }
            }
            catch (Exception e)
            {
                json.code = 505;
                json.msg = e.ToString();
            }

            return Json(json);
        }


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="customeruser">要查询的客户名称</param>
        /// <param name="page">页面</param>
        /// <param name="rows">行数</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult QueryCustomer(String customeruser = "", int page = 1, int rows = 10)
        {
            Euitable<xr_bl_clientinfo> table = new Euitable<xr_bl_clientinfo>();
            //设置查询条件
            Func<xr_bl_clientinfo, bool> where = x => x.client_name.Contains(customeruser);
            Func<xr_bl_clientinfo, bool> where2 = x => x.validflag == "1";
            table.rows = db.xr_bl_clientinfo
                .Where(where).Where(where2)
                .OrderBy(y => y.clientinfo_uuid)
                .Skip<xr_bl_clientinfo>(rows * (page - 1))
                .Take<xr_bl_clientinfo>(rows)
                .ToList<xr_bl_clientinfo>();
            table.total = db.xr_bl_clientinfo
                .Where(where).Where(where2)
                .Count();

            return Json(table);
        }

        /// <summary>
        /// 查询项目信息
        /// </summary>
        /// <param name="uuid"></param>
        /// <param name="projectname"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult QueryProject(String uuid,String projectname = "", int page = 1, int rows = 10)
        {
            Euitable<xr_bl_proinfo> table = new Euitable<xr_bl_proinfo>();
            //设置查询条件
            Func<xr_bl_proinfo, bool> where = x => x.pro_name.Contains(projectname);
            Func<xr_bl_proinfo, bool> where2 = x => x.validflag == "1";
            Func<xr_bl_proinfo, bool> where3 = x => x.clientinfo_uuid==uuid;
            table.rows = db.xr_bl_proinfo
                .Where(where).Where(where2).Where(where3)
                .OrderBy(y => y.clientinfo_uuid)
                .Skip<xr_bl_proinfo>(rows * (page - 1))
                .Take<xr_bl_proinfo>(rows)
                .ToList<xr_bl_proinfo>();
            table.total = db.xr_bl_proinfo
                .Where(where).Where(where2).Where(where3)
                .Count();

            return Json(table);
        }

        [HttpPost]
        public ActionResult Addproject(String projectstr,String uuid)
        {
            JR json = new JR();
            if (projectstr.Equals(null) || projectstr == "")
            {
                json.code = 500;
                json.msg = "您什么也没有输入!";
                return Json(json);
            }
                else if (uuid.Equals(null) || uuid == "")
            {
                json.code = 500;
                json.msg = "客户UUID不存在!";
                return Json(json);
            }
            try
            {
                projectstr = HttpUtility.UrlDecode(projectstr);
                xr_bl_proinfo info = new xr_bl_proinfo();
                info.clientinfo_uuid = uuid;
                info.proinfo_uuid = Tools.getGuid();
                info.pro_name = projectstr;
                info.validflag = "1";
                db.xr_bl_proinfo.Add(info);
                if (db.SaveChanges() == 1)
                {
                    json.code = 200;
                    json.msg = "保存成功!";
                }
                else
                {
                    json.code = 300;
                    json.msg = "保存失败，请重试或与管理员联系！";
                }
            }
            catch (Exception e)
            {
                json.code = 505;
                json.msg = e.ToString();
            }

            return Json(json);
        }

        [HttpPost]
        public ActionResult EditProject(String uuid, String projectname)
        {
            JR json = new JR();
            if (projectname.Equals(null) || uuid.Equals(null) || projectname == "" || uuid == "")
            {
                json.code = 500;
                json.msg = "请输入有效值";
                return Json(json);
            }
            try
            {
                projectname = HttpUtility.UrlDecode(projectname);
                xr_bl_proinfo info = db.xr_bl_proinfo.FirstOrDefault(x => x.proinfo_uuid == uuid);
                info.pro_name = projectname;
                db.Entry(info).State = EntityState.Modified;
                if (db.SaveChanges() == 1)
                {
                    json.code = 200;
                    json.msg = "修改成功!";
                }
                else
                {
                    json.code = 300;
                    json.msg = "修改失败，请重试或与管理员联系！";
                }
            }
            catch (Exception e)
            {
                json.code = 505;
                json.msg = e.ToString();
            }

            return Json(json);
        }

        /// <summary>
        /// 删除项目
        /// </summary>
        /// <param name="uuid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelProject(String uuid)
        {
            JR json = new JR();
            if (uuid.Equals(null) || uuid == "")
            {
                json.code = 500;
                json.msg = "请选择要删除的客户";
                return Json(json);
            }
            try
            {
                xr_bl_proinfo info = db.xr_bl_proinfo.FirstOrDefault(x => x.proinfo_uuid == uuid);
                info.validflag = "0";
                db.Entry(info).State = EntityState.Modified;
                if (db.SaveChanges() == 1)
                {
                    json.code = 200;
                    json.msg = "删除成功!";
                }
                else
                {
                    json.code = 300;
                    json.msg = "删除失败，请重试或与管理员联系！";
                }
            }
            catch (Exception e)
            {
                json.code = 505;
                json.msg = e.ToString();
            }

            return Json(json);
        }
    }
}